﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;

namespace BusinessLogic.Command
{
    public class MaintainEducationCommand : BaseCommand<object>
    {
        private List<APP_EDUCATION> _list;

        public MaintainEducationCommand(List<APP_EDUCATION> educationList)
        {
            _list = educationList;
        }

        public override void Command()
        {
            CubeOfDB2Entities ctx = new CubeOfDB2Entities();

            foreach (APP_EDUCATION row in _list)
            {
                if (row.act == ActionEnum.Add)
                {
                    ctx.APP_EDUCATION.AddObject(row);
                }
                else if (row.act == ActionEnum.Edit)
                {
                    APP_EDUCATION e = ctx.APP_EDUCATION.Where(o => o.ID == row.ID).FirstOrDefault();
                    if (e != null)
                    {
                        e.PERSONAL_ID = row.PERSONAL_ID;
                        e.DEGREE = row.DEGREE;
                        e.INSTITUTE_NAME = row.INSTITUTE_NAME;
                        e.FROM_YEAR = row.FROM_YEAR;
                        e.TO_YEAR = row.TO_YEAR;
                        e.GPA = row.GPA;
                        e.MAJOR = row.MAJOR;
                    }
                    else
                        ctx.APP_EDUCATION.AddObject(row);
                }
                else if (row.act == ActionEnum.Delete)
                {
                    APP_EDUCATION e = ctx.APP_EDUCATION.Where(o => o.ID == row.ID).FirstOrDefault();
                    if (e != null)
                    {
                        ctx.APP_EDUCATION.DeleteObject(e);
                    }
                }

                ctx.SaveChanges();
            }
        }
    }

}
